var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    navList: [],
    categoryList: [],
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
      photourl:'http://f10.ylwxmall.com:6035/photo/%E5%A5%87%E5%BC%BA%E9%80%9F%E6%B4%81%E6%B4%97%E8%A1%A3%E7%B2%89700g..jpg',
  },
  onLoad: function (options) {
    this.getCatalog();
  },
  getCatalog: function () {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
      // wx.request({
      //     // url: 'https://f10.ylwxmall.com/shopping/api/producttype/get_listcategory',
      //     url:'http://huanqiuxiaozhen.com/wemall/slider/list',
      //     method: 'GET',
      //     data: {},
      //     header: {
      //         'Accept': 'application/json'
      //     },
      //     success: function (res) {
      //         console.log(res)
      //         // const list = res.data.filter(it => it.fldparentid == 0);
      //         // that.setData({
      //         //     categoryList: res.data,
      //         //     navList: list,
      //         //     currentCategory: list.filter(it=>it.fldparentid == list[0].fldtypeid)
      //         // });
      //         // wx.hideLoading();
      //     }
      // })
    util.request(api.CatalogList).then(function (res) {
        const list = res.data.filter(it => it.fldparentid == 0);
        that.setData({
          categoryList: res.data,
          navList: list,
          currentCategory: list.filter(it=>it.fldparentid == list[0].fldtypeid)
        });
        wx.hideLoading();
      });
    util.request(api.GoodsCount).then(function (res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });

  },
  getCurrentCategory: function (id) {
    let that = this;
    util.request(api.CatalogCurrent, { id: id })
      .then(function (res) {
        that.setData({
          currentCategory: res.data.currentCategory
        });
      });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  getList: function () {
    var that = this;
    util.request(api.ApiRootUrl + 'api/catalog/' + that.data.currentCategory.cat_id)
      .then(function (res) {
        that.setData({
          categoryList: res.data,
        });
      });
  },
  switchCate: function (event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }

    // this.getCurrentCategory(event.currentTarget.dataset.id);
      const currentCategory = this.data.categoryList.filter(it=>it.fldparentid == currentTarget.dataset.id);
      currentCategory.forEach(item=>{item.subCategoryList = that.sonsTree(this.data.categoryList,item.fldtypeid)})
      currentCategory.forEach(item=>item.subCategoryList.forEach(it=>it.photourl = it.fldphotourl ? 'https://f10.ylwxmall.com/image/'+it.fldphotourl.split('|')[1].replace('%','%25') : 'https://f10.ylwxmall.com/image/AD/noimage.jpg'))
      that.setData({
          currentCategory: currentCategory
      });
  },
    sonsTree: function (arr,id){
    var temp = [],level=1;
    var forFn = function(arr, id,level){
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i];
            if (item.fldparentid==id) {
                item.level=level;
                temp.push(item);
                forFn(arr,item.fldtypeid,level+1);
            }
        }
    };
    forFn(arr, id,level);
    return temp;
},
    photoUrl:function(item){
        // return 'http://f10.ylwxmall.com:6035/photo/%E5%A5%87%E5%BC%BA%E9%80%9F%E6%B4%81%E6%B4%97%E8%A1%A3%E7%B2%89700g..jpg'
        return  item.fldphotourl ? 'http://f10.ylwxmall.com:6035/photo/'+item.fldphotourl.split('|')[1] : 'http://f10.ylwxmall.com:6035/photo/%E5%A5%87%E5%BC%BA%E9%80%9F%E6%B4%81%E6%B4%97%E8%A1%A3%E7%B2%89700g..jpg';
        // return 'http://f10.ylwxmall.com:6035/photo/'+item.fldphotourl.split('|')[1]
        console.log(item)
    },
})